package com.xxs.controller.admin;

import java.util.Enumeration;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;

import com.jfinal.aop.Before;
import com.xxs.interceptor.AdminInterceptor;
import com.xxs.utils.UUIDUtils;
import com.xxs.utils.xml.Fields;
import com.xxs.utils.xml.XmlDom4jUtils;

@Before(AdminInterceptor.class)
@SuppressWarnings("unchecked")
public class NodeController extends BaseAdminSiteController {
	
	public void add(){
//		String nodeId = getPara(0);
//		String modelInfoId = getPara(1);
//		String siteCode = getCurrentSiteCode();
//		if(nodeId!=null){
//			List<Element> infoElements = XmlDom4jUtils.getElements(getXmlPath(true),null);
//		}else{
//			
//		}
		
		String id = getPara(0);
		String modelName = getModeName();
		Element element = null;
		Document document = XmlDom4jUtils.getDocument(getXmlPath(getCurrentSiteCode(), modelName));
		List<Element> listElements = document.selectNodes("//"+modelName+"[@id='"+id+"']");
		if(listElements!=null && listElements.size()>0){
			element = listElements.get(0);
		}
		setAttr("fieldList", getFields());
		setAttr("id", id);
		setAttr("element", element);
		
		
	}
	public void save(){
		String parentId = getPara(0);
    	if(parentId!=null && !parentId.equals("")){
    		String modelName = getModeName();
    		List<Fields> fieldList = getFields();
        	Document document = XmlDom4jUtils.getDocument(getXmlPath(true));
        	List<Element> listElements = document.selectNodes("//"+modelName+"[@id='"+parentId+"']");
        	Element element = null;
        	if(listElements!=null && listElements.size()>0){
        		element = listElements.get(0);
        		Element newElement = element.addElement(modelName);
        		for (int i = 0; i < fieldList.size(); i++) {
        			System.out.println("当前循环的属性名："+fieldList.get(i).getName()+"填充的内容"+getPara(fieldList.get(i).getName()));
        			if("id".equals(fieldList.get(i).getName())){
        				newElement.addAttribute(fieldList.get(i).getName(), UUIDUtils.creatUUID());
        			}else{
        				newElement.addAttribute(fieldList.get(i).getName(), getPara(fieldList.get(i).getName()));
        			}
        		}
            	try {
            		XmlDom4jUtils.saveDocumentToFile(modelName, document);
            	} catch (Exception e) {
            		e.printStackTrace();
            	}
        	}
    	}else{
    		baseSave();
    	}
    	list();
	}
	public void update(){
		String id = getPara(0);
		String modelName = getModeName();
		Element element = null;
    	Document document = XmlDom4jUtils.getDocument(getXmlPath(getCurrentSiteCode(), modelName));
    	List<Element> listElements = document.selectNodes("//"+modelName+"[@id='"+id+"']");
    	if(listElements!=null && listElements.size()>0){
    		element = listElements.get(0);
    		Enumeration<String> enumeration = getParaNames();
        	while(enumeration.hasMoreElements()){
        		String name=(String)enumeration.nextElement();
        		String value=getPara(name);
        		System.out.print(name + "=" + value);
        		Attribute attribute = element.attribute(name);  
        		if(attribute==null){
        			element.addAttribute(name, value);
        		}else{
        			attribute.setValue(value);
        		}
        	}
        	try {
        		XmlDom4jUtils.saveDocumentToFile(getXmlPath(getCurrentSiteCode(), modelName), document);
        	} catch (Exception e) {
        		e.printStackTrace();
        	}
    	}
    	list();
    }
}
